package JDBC;

import javax.xml.bind.Element;
import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo {
    public static void main(String[] args) throws Exception {
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎注册");
        System.out.println(userInfo);

    }

    public static int reg(UserInfo userInfo) throws Exception {

        Class.forName("com.mysql.cj.jdbc.Driver");
        //2
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root", "root");
        System.out.println("成功链接数据库");
//成功链接数据库
        Statement statement = connection.createStatement();


        String sql = "INSERT INTO userinfo (username,password,nickname,age) " +
                "VALUE ('" + userInfo.getUsername() + "','" + userInfo.getPassword() + "','" + userInfo.getNickname() + "'," + userInfo.getAge() + ")";
        System.out.println(sql);

        int num = statement.executeUpdate(sql);
        if (num > 0) {
            System.out.println("插入完毕!");

        }
        return num;
    }
}




